import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.TreeSet;

import processing.core.PApplet;

import controlP5.ControlEvent;
import controlP5.ControlP5;
import controlP5.ListBox;
import controlP5.RadioButton;
import controlP5.Toggle;


public class DataControlGroup {
	
	ArrayList<String> cleanUpList; //MOVE LATER
	ControlP5 theControlP5;
	PApplet theApplet;
	public CSVConn myconn;
	
	public DataControlGroup( ControlP5 controlP5, PApplet p, int xPos, int yPos) {
		theApplet = p;
		theControlP5 = controlP5;
		
		MainGroup g = new MainGroup( "Data", p, controlP5, xPos + 300, yPos + 50, 200, 50);
		Main.groupList.add(g);
		g.theGroup.setLabel("Data Control Group");
		
		cleanUpList = new ArrayList<String>();
		
		RadioButton r;
		  r = controlP5.addRadioButton("dataType",20,10);
		  r.setColorForeground(theApplet.color(120,50,0));
		  r.setColorActive(theApplet.color(255));
		  r.setColorLabel(theApplet.color(0,0,100));
		  r.setItemsPerRow(1);
		  r.setId(Main.nextId++);
		  Main.nameToId.put("dataTypeRadio", r.id());

		  addToRadioButton(r,"Genes", Main.nextId++);
		  addToRadioButton(r,"Annotations", Main.nextId++);
		  addToRadioButton(r,"Expression", Main.nextId++);
		r.setGroup("Data");		
	}
	void addToRadioButton(RadioButton theRadioButton, String theName, int theValue ) {
		Toggle t = theRadioButton.addItem(theName,theValue);
	  	t.captionLabel().setColorBackground(theApplet.color(80));
	  	t.captionLabel().style().movePadding(2,0,-1,2);
	  	t.captionLabel().style().moveMargin(-2,0,0,-3);
	  	t.captionLabel().style().backgroundWidth = 70;
	}
	
	void controlEvent(ControlEvent theEvent) {
		if (theEvent.isGroup()) {
			int theId = theEvent.group().id();
		    // an event from a group e.g. scrollList
			if(theId == Main.nameToId.get("dataTypeRadio")) {
				setDataOptions((int)theEvent.group().value());
				return;
			}
			else {
				if(Main.nameToId.get("geneSelectorList") != null && theId == Main.nameToId.get("geneSelectorList")) {
					ListBox l = (ListBox) theControlP5.getGroup("geneSelectorList");
					String geneName = Main.idToName.get((int)theEvent.group().value());
					l.removeItem(geneName);
					ListBox l2 = (ListBox) theControlP5.getGroup("selectedGenes");
					l2.addItem(geneName, Main.nextId);
					Main.idToName.put(Main.nextId, geneName);
					Main.nameToId.put(geneName, Main.nextId++);
					return;
				}
				else if(Main.nameToId.get("annotationSelectorList") != null && theId == Main.nameToId.get("annotationSelectorList")) {
					ListBox l = (ListBox) theControlP5.getGroup("annotationSelectorList");
					String annotationName = Main.idToName.get((int)theEvent.group().value());
					l.removeItem(annotationName);
					ListBox l2 = (ListBox) theControlP5.getGroup("selectedAnnotations");
					l2.addItem(annotationName, Main.nextId);
					Main.idToName.put(Main.nextId, annotationName);
					Main.nameToId.put(annotationName, Main.nextId++);
					testDataBase(annotationName);
					//showSelectAnnotationsGroup(annotationName);
					return;
				}
				else if(Main.nameToId.get("dataSelectorList") != null && theId == Main.nameToId.get("dataSelectorList")) {
					ListBox l = (ListBox) theControlP5.getGroup("dataSelectorList");
					String dataName = Main.idToName.get((int)theEvent.group().value());
					l.removeItem(dataName);
					ListBox l2 = (ListBox) theControlP5.getGroup("selectedData");
					l2.addItem(dataName, Main.nextId);
					Main.idToName.put(Main.nextId, dataName);
					Main.nameToId.put(dataName, Main.nextId++);		
					showDataDistribution(dataName);
					return;
				}
			}
		 }		
	}
	void testDataBase(String annotationName) {
		if(myconn.query("" +
				"SELECT Eigenarray2 FROM partial_genes4329 WHERE Eigenarray2 <= 0"	
		) == 1) {
			ResultSet results = myconn.getResults();	
			try
			{
				// dump out the results
				while (results.next())
				{
					System.out.println(results.getString("Eigenarray2"));					
				}
			}
			catch(Exception e)
			{
				System.out.println("Error in Main-> " + e);
			}
		}
	}
	void showDataDistribution(String dataName) {
		MainGroup g = new MainGroup( "Data Distribution for: " + dataName, theApplet, theControlP5, 50, 50, 300, 300);
		Main.groupList.add(g);
		DataDistributionObject c = new DataDistributionObject(dataName, theApplet, g.theCanvas, g.theGroup, 50, 50, 300,300);
		g.theCanvas.drawQueue.add(c);
		g.theCanvas.parent.theGroup.addCanvas(c);
		g.theGroup.addCloseButton();
	}
	void showSelectAnnotationsGroup(String annotation) {
		RadioButton r;
		  r = theControlP5.addRadioButton("annotationList",10,250);
		  r.setColorForeground(theApplet.color(120,50,0));
		  r.setColorActive(theApplet.color(255));
		  r.setColorLabel(theApplet.color(0,0,100));
		  r.setLabel("Genes to Show for " + annotation + " annotation");
		  r.setItemsPerRow(1);
		  r.setId(Main.nextId++);
		  Main.nameToId.put("annotationRadio", r.id());
		  
		myconn = new CSVConn("../data");
		//if(myconn.query("SELECT DISTINCT " + annotation  + " FROM partial_annotations4329") == 1) {
		//DISTINCT command not working, update code if database functionality is switched.
		if(myconn.query("SELECT " + annotation  + " FROM partial_annotations4329") == 1) {
			ResultSet results = myconn.getResults();
			TreeSet<String> s = new TreeSet<String>();  //need to use set to get unique data elements
			try
			{
				while (results.next())
				{
					s.add(results.getString(annotation));
				}
				if( s.size() < 7) 
				{
					for (String str: s) 
					{
						addToRadioButton(r, str ,Main.nextId);
						Main.idToName.put(Main.nextId, str);
						Main.nameToId.put(str, Main.nextId++);
					}
				}
			}
			catch(Exception e)
			{
				System.out.println("Error in Main-> " + e);
			}
		}
		r.setGroup("Data");
	}
	void setDataOptions(int dataTypeRadioValue) {
		switch(dataTypeRadioValue) {
		case(1): 
			Main.groupList.get(1).theGroup.setBackgroundHeight(400);
		
			for(String s: cleanUpList) {
				theControlP5.group(s).setVisible(false);
			}
		
			if(theControlP5.group("geneSelectorList") == null) {
				ListBox slist = theControlP5.addListBox("geneSelectorList",10,75,80,180);
				slist.setLabel("Gene List");
				slist.setGroup("Data");
				slist.setId(Main.nextId);
				Main.idToName.put(Main.nextId, "geneSelectorList");
				Main.nameToId.put("geneSelectorList", Main.nextId++);
				cleanUpList.add("geneSelectorList");
				
				ListBox selectedGenes = theControlP5.addListBox("selectedGenes",105,75,80,180);
				selectedGenes.setLabel("Selected");
				selectedGenes.setGroup("Data");
				selectedGenes.setId(Main.nextId);
				Main.idToName.put(Main.nextId, "selectedGenes");
				Main.nameToId.put("selectedGenes", Main.nextId++);
				cleanUpList.add("selectedGenes");
				
				myconn = new CSVConn("../data");		
				if(myconn.query("SELECT * FROM partial_genes4329") == 1) {
					ResultSet results = myconn.getResults();	
					try
					{
						// dump out the results
						while (results.next())
						{
							slist.addItem(results.getString("Gene"), Main.nextId);
							Main.idToName.put(Main.nextId, results.getString("Gene"));
							Main.nameToId.put(results.getString("Gene"), Main.nextId++);							
						}
					}
					catch(Exception e)
					{
						System.out.println("Error in Main-> " + e);
					}
				}
			}
			else if( theControlP5.group("geneSelectorList").isVisible() == false) {
				theControlP5.group("geneSelectorList").setVisible(true);
				theControlP5.group("selectedGenes").setVisible(true);
			}
		break;
		case(2):
			Main.groupList.get(1).theGroup.setBackgroundHeight(400);	
		
			for(String s: cleanUpList) {
				theControlP5.group(s).setVisible(false);
			}
			
			if(theControlP5.group("annotationSelectorList") == null) {				
				 ListBox slist = theControlP5.addListBox("annotationSelectorList",10,75,160,60);
				 slist.setLabel("Annotation List");
				 slist.setGroup("Data");
				 slist.setId(Main.nextId);
				 Main.idToName.put(Main.nextId, "annotationSelectorList");
				 Main.nameToId.put("annotationSelectorList", Main.nextId++);
				 cleanUpList.add("annotationSelectorList");
				
				 ListBox selectedAnnotations = theControlP5.addListBox("selectedAnnotations",10,170,160,60);
				 selectedAnnotations.setLabel("Selected");
				 selectedAnnotations.setGroup("Data");
				 selectedAnnotations.setId(Main.nextId);
				 Main.idToName.put(Main.nextId, "selectedAnnotations");
				 Main.nameToId.put("selectedAnnotations", Main.nextId++);
				 cleanUpList.add("selectedAnnotations");
				 
				 
				myconn = new CSVConn("../data");		
				if(myconn.query("SELECT * FROM partial_annotations4329") == 1) {
					ResultSet results = myconn.getResults();
					try
					{
						// dump out the results
						ResultSetMetaData rsMetaData = results.getMetaData();
						int numberOfColumns = rsMetaData.getColumnCount();
						String[] annotationNames = new String[numberOfColumns];
						 for (int i = 1; i <= numberOfColumns; i++) {
							 if(!rsMetaData.getColumnLabel(i).equals("")) {
								 annotationNames[i-1] = rsMetaData.getColumnLabel(i);
								 slist.addItem(annotationNames[i-1], Main.nextId);
								 Main.idToName.put(Main.nextId, annotationNames[i-1]);
								 Main.nameToId.put(annotationNames[i-1], Main.nextId++);
							     System.out.println(rsMetaData.getColumnLabel(i));
							 }
						 }
					}
					catch(Exception e)
					{
						System.out.println("Error in Main-> " + e);
					}
				}
			}
			else if( theControlP5.group("annotationSelectorList").isVisible() == false) {
				theControlP5.group("annotationSelectorList").setVisible(true);
				theControlP5.group("selectedAnnotations").setVisible(true);
			}			
		break;
		case(3):
			Main.groupList.get(1).theGroup.setBackgroundHeight(400);
		
			for(String s: cleanUpList) {
				theControlP5.group(s).setVisible(false);
			}
			
			if(theControlP5.group("dataSelectorList") == null) {				
				 ListBox slist = theControlP5.addListBox("dataSelectorList",10,75,160,60);
				 slist.setLabel("Data List");
				 slist.setGroup("Data");
				 slist.setId(Main.nextId);
				 Main.idToName.put(Main.nextId, "dataSelectorList");
				 Main.nameToId.put("dataSelectorList", Main.nextId++);
				 cleanUpList.add("dataSelectorList");
				
				 ListBox selectedData = theControlP5.addListBox("selectedData",10,170,160,60);
				 selectedData.setLabel("Selected");
				 selectedData.setGroup("Data");
				 selectedData.setId(Main.nextId);
				 Main.idToName.put(Main.nextId, "selectedData");
				 Main.nameToId.put("selectedData", Main.nextId++);
				 cleanUpList.add("selectedData");
				 
				 
				myconn = new CSVConn("../data");		
				if(myconn.query("SELECT * FROM partial_genes4329") == 1) {
					ResultSet results = myconn.getResults();
					try
					{
						// dump out the results
						ResultSetMetaData rsMetaData = results.getMetaData();
						int numberOfColumns = rsMetaData.getColumnCount();
						String[] dataNames = new String[numberOfColumns];
						 for (int i = 1; i <= numberOfColumns; i++) {
							 if(!rsMetaData.getColumnLabel(i).equals("")) {
								 dataNames[i-1] = rsMetaData.getColumnLabel(i);
								 slist.addItem(dataNames[i-1], Main.nextId);
								 Main.idToName.put(Main.nextId, dataNames[i-1]);
								 Main.nameToId.put(dataNames[i-1], Main.nextId++);
							     System.out.println(rsMetaData.getColumnLabel(i));
							 }
						 }
					}
					catch(Exception e)
					{
						System.out.println("Error in Main-> " + e);
					}
				}
			}
			else if( theControlP5.group("dataSelectorList").isVisible() == false) {
				theControlP5.group("dataSelectorList").setVisible(true);
				theControlP5.group("selectedData").setVisible(true);
			}		
		break;
		default:
			Main.groupList.get(1).theGroup.setBackgroundHeight(50);
		
			for(String s: cleanUpList) {
				theControlP5.group(s).setVisible(false);
			}
		}
		
	}

}
